package com.cgtong.android.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.cgtong.android.R;
import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.util.CommonUtils;
import com.cgtong.android.widget.GlideApp;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chunc on 2018/1/3.
 */

public class RecycleListAdapter extends BaseQuickAdapter<ProblemBean, BaseViewHolder> {
    public static final String TAG = RecycleListAdapter.class.getSimpleName();
    private Context mContext;
    private SparseArray<ProblemBean> mRecycleArray;
    private LayoutInflater mLayoutInflater;

    private boolean isShowSelectBtn = false;

    public void toggleShowSelectBtn() {
        isShowSelectBtn = !isShowSelectBtn;
    }

    public boolean isShowSelectBtn() {
        return isShowSelectBtn;
    }

    public void setShowSelectBtn(boolean showSelectBtn) {
        this.isShowSelectBtn = showSelectBtn;
    }

    public RecycleListAdapter(Context context, @Nullable List<ProblemBean> data, SparseArray<ProblemBean> recycleList) {
        super(data);
        this.mContext = context;
        this.mRecycleArray = recycleList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }
    @Override
    protected MyViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(mLayoutInflater.inflate(R.layout.adapter_recycle_list, parent, false));
    }

    @Override
    protected void convert(BaseViewHolder holder, final ProblemBean item) {
        final MyViewHolder helper = (MyViewHolder) holder;
        if (item.getAvasterId() >= 1 && item.getAvasterId() <= 12) {
            CommonUtils.setUserAvatar(mContext, helper.avatarImg, item.getAvasterId());
        } else {
            LogUtils.d("avatarId错误");
            helper.avatarImg.setImageResource(R.drawable.head_1);
        }
        helper.nickName.setText(item.getNickName());
        helper.dateTxt.setText(item.getCreateTime());
        helper.addressTxt.setText(item.getAddress());

        //展示图片
        List<ProblemBean.ResourceBean> picList = item.getResourceList();
        if (picList != null && picList.size() > 0) {
            helper.pictureGVi.setVisibility(View.VISIBLE);
            helper.pictureGVi.setAdapter(new ProblemPictureAdapter(picList, item));
        } else {
            helper.pictureGVi.setVisibility(View.GONE);
        }

        //录音
        if (!TextUtils.isEmpty(item.getVoiceUrl()) && item.getVoiceUrl().startsWith("http")) {
            helper.audioLl.setVisibility(View.VISIBLE);
            helper.playAudioDurationTxt.setText(item.getVoiceTime() + "\"");
        } else {
            helper.audioLl.setVisibility(View.GONE);
        }
        //文字
        if (!TextUtils.isEmpty(item.getDesc())) {
            helper.contentTxt.setVisibility(View.VISIBLE);
            helper.contentTxt.setText(item.getDesc());
        } else {
            helper.contentTxt.setVisibility(View.GONE);
        }

        //判断是否展示选择按钮
        if (isShowSelectBtn) {
            helper.checkBox.setVisibility(View.VISIBLE);
            final int position = helper.getAdapterPosition();
            helper.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        mRecycleArray.put(position, item);
                    } else {
                        mRecycleArray.delete(position);
                    }
                }
            });
            helper.getView(R.id.content_ll).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (helper.checkBox.isChecked()) {
                        helper.checkBox.setChecked(false);
                    }else{
                        helper.checkBox.setChecked(true);
                    }
                }
            });
            ProblemBean beanOfArray = mRecycleArray.get(position);
            if (beanOfArray != null && beanOfArray.getId() == item.getId()) {
                Log.d(TAG, "存在集合中：" + item.getDesc() + " 位置：" + helper.getAdapterPosition());
                helper.checkBox.setChecked(true);
            } else {
                Log.d(TAG, "不存在集合中：" + item.getDesc() + " 位置：" + helper.getAdapterPosition());
                helper.checkBox.setChecked(false);
            }


        } else {
            helper.checkBox.setVisibility(View.GONE);
        }


    }

    public static class MyViewHolder extends BaseViewHolder {

        @BindView(R.id.select_item_img)
        CheckBox checkBox;
        @BindView(R.id.avatar_img)
        ImageView avatarImg;
        @BindView(R.id.name_txt)
        TextView nickName;
        @BindView(R.id.date_txt)
        TextView dateTxt;
        @BindView(R.id.location_txt)
        TextView addressTxt;
        @BindView(R.id.play_audio_duration_txt)
        TextView playAudioDurationTxt;
        @BindView(R.id.content_txt)
        TextView contentTxt;
        @BindView(R.id.audio_ll)
        LinearLayout audioLl;
        @BindView(R.id.grid_view)
        GridView pictureGVi;


        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

        }
    }

    class ProblemPictureAdapter extends BaseAdapter {

        private List<ProblemBean.ResourceBean> mList;
        private ProblemBean mProblemBean;

        public ProblemPictureAdapter(List<ProblemBean.ResourceBean> mList, ProblemBean problemBean) {
            this.mList = mList;
            this.mProblemBean = problemBean;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public ProblemBean.ResourceBean getItem(int position) {
            if (position <= mList.size() - 1) {
                return mList.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.layout_picture, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ProblemBean.ResourceBean item = getItem(position);
            if (item != null) {
                GlideApp.with(mContext).load(getItem(position).getSmallContent()).dontAnimate()
                        .placeholder(R.drawable.icon_default_loading)
                        .error(R.drawable.icon_default_error).into(holder.imageView);

                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        ImagePreviewActivity.start(mContext, mList, position);
//                        if (!isShowSelectBtn) {
//                        boolean canDelete = false;
//                        boolean canEvaluate = false;
//                        if (App.getInstance().isUserLogin()) {
//                            if ((mProblemBean.getStatus() == 4 || mProblemBean.getStatus() == 5 || mProblemBean.getStatus() == 7) && App.getInstance().isOwnself(mProblemBean.getCreateUserId())) {
//                                canDelete = true;
//                            }
//                            if ((mProblemBean.getStatus() == 4 || mProblemBean.getStatus() == 5) && App.getInstance().isOwnself(mProblemBean.getCreateUserId())) {
//                                canEvaluate = true;
//                            }
//                        }
//                        ProblemDetailActivity.start(mContext, mProblemBean.getId(), canDelete, canEvaluate);
//                        }

                    }
                });
            } else {
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        if (!isShowSelectBtn) {
//                        boolean canDelete = false;
//                        boolean canEvaluate = false;
//                        if (App.getInstance().isUserLogin()) {
//                            if ((mProblemBean.getStatus() == 4 || mProblemBean.getStatus() == 5 || mProblemBean.getStatus() == 7) && App.getInstance().isOwnself(mProblemBean.getCreateUserId())) {
//                                canDelete = true;
//                            }
//                            if ((mProblemBean.getStatus() == 4 || mProblemBean.getStatus() == 5) && App.getInstance().isOwnself(mProblemBean.getCreateUserId())) {
//                                canEvaluate = true;
//                            }
//                        }
//                        ProblemDetailActivity.start(mContext, mProblemBean.getId(), canDelete, canEvaluate);
//                        }
                    }
                });
            }
            return convertView;
        }


        class ViewHolder {

            @BindView(R.id.img_view)
            ImageView imageView;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }


}
